﻿using System.Collections.Generic;
using System.Data.SqlClient;
using Abs.Core.Models.Room;

namespace Abs.Core.DAL.AdoNet
{
    internal class RoomScheduleRepository : AdoNetRepository<RoomSchedule>
    {
        public override bool Create(RoomSchedule item)
        {
            return this.CreateOrUpdate(item, StoredProcedures.CreateRoomSchedule);
        }//end of public override bool Create(RoomSchedule item)

        public override IList<RoomSchedule> SelectAll()
        {
            var roomSchedules = new List<RoomSchedule>();
            this.SqlCommand.CommandText = StoredProcedures.GetRoomSchedules;
            var dataReader = this.ExecuteReader();

            if( dataReader == null )
                return roomSchedules;

            while (dataReader.Read())
            {
                roomSchedules.Add(new RoomSchedule
                {
                    RoomId = (int) dataReader["RoomId"],
                    DayId = (int) dataReader["DayId"],
                    StartTime = dataReader.GetDateTime((int) RoomScheduleField.StartTime),
                    EndTime = dataReader.GetDateTime((int)RoomScheduleField.EndTime)
                });
            }//end of if (dataReader.Read() == true)
            return roomSchedules;
        }//end of public override IList<RoomSchedule> SelectAll()

        /// <summary>
        ///     Get RoomSchedule based on the given RoomId and 
        ///     DayId as primary key.
        /// </summary>
        /// <param name="id">
        ///     Primary Key
        ///     id[0] represents "RoomId".
        ///     id[1] represents "DayId"
        /// </param>
        /// <returns>
        ///     Return an user, if successfully deleting the item.
        ///     Otherwise return null.
        /// </returns>
        public override RoomSchedule Select(object[] id)
        {
            var roomId = new SqlParameter("RoomId", id[0]);
            var dayId = new SqlParameter("DayId", id[1]);

            this.SqlCommand.CommandText = StoredProcedures.GetRoomSchedule;
            this.SqlCommand.Parameters.Add(roomId);
            this.SqlCommand.Parameters.Add(dayId);

            var dataReader = this.ExecuteReader();

            if( dataReader == null )
                return null;

            if (dataReader.Read())
            {
                return new RoomSchedule
                {
                    RoomId = (int)dataReader["RoomId"],
                    DayId = (int)dataReader["DayId"],
                    StartTime = dataReader.GetDateTime((int)RoomScheduleField.StartTime),
                    EndTime = dataReader.GetDateTime((int)RoomScheduleField.EndTime)
                };
            }//end of if (dataReader.Read() == true)
            return null;
        }//end of public override RoomSchedule Select(object[] id)

        public override bool Update(RoomSchedule item)
        {
            return this.CreateOrUpdate(item, StoredProcedures.EditRoomSchedule);
        }//end of public override bool Update(RoomSchedule item)

        /// <summary>
        /// Delete RoomSchedule based on the given RoomId and DayId as primary keys.
        /// </summary>
        /// <param name="id">
        ///     id[0] represents RoomId. id[1] represents DayId.
        /// </param>
        /// <returns></returns>
        public override bool Delete(object[] id)
        {
            var roomId = new SqlParameter("RoomId", id[0]);
            var dayId = new SqlParameter("DayId", id[1]);

            this.SqlCommand.CommandText = StoredProcedures.DeleteRoomSchedule;
            this.SqlCommand.Parameters.Add(roomId);
            this.SqlCommand.Parameters.Add(dayId);

            return this.ExecuteNonQuery();
        }//end of public override bool Delete(object[] id)

        internal bool DeleteRoomScheduleByRoomId(int roomId)
        {
            this.SqlCommand.CommandText = StoredProcedures.DeleteRoomScheduleByRoomId;
            this.SqlCommand.Parameters.Add(new SqlParameter("RoomId", roomId));

            return this.ExecuteNonQuery();
        }//end of internal bool DeleteRoomScheduleByRoomId(int roomId)

        internal IList<RoomSchedule> GetRoomsOnDay(int id)
        {
            var roomSchedules = new List<RoomSchedule>();
            var dayId = new SqlParameter("DayId", id);

            this.SqlCommand.CommandText = StoredProcedures.GetRoomsOnDay;
            this.SqlCommand.Parameters.Add(dayId);

            var dataReader = this.ExecuteReader();

            if( dataReader == null )
                return null;

            while(dataReader.Read())
            {
                roomSchedules.Add(new RoomSchedule
                                  {
                                      DayId = dataReader.GetInt32((int) RoomScheduleField.DayId),
                                      RoomId = dataReader.GetInt32((int) RoomScheduleField.RoomId),
                                      StartTime = dataReader.GetDateTime((int) RoomScheduleField.StartTime),
                                      EndTime = dataReader.GetDateTime((int) RoomScheduleField.EndTime)
                                  });
            }//end of while(dataReader.Read())
            return roomSchedules;
        }//end of internal IList<RoomSchedule> GetRoomsOnDay(int id)

        protected bool CreateOrUpdate(RoomSchedule item, string command)
        {
            var roomId = new SqlParameter("RoomId", item.RoomId);
            var dayId = new SqlParameter("DayId", item.DayId);
            var startTime = new SqlParameter("StartTime", item.StartTime);
            var endTime = new SqlParameter("EndTime", item.EndTime);

            this.SqlCommand.CommandText = command;
            this.SqlCommand.Parameters.Add(roomId);
            this.SqlCommand.Parameters.Add(dayId);
            this.SqlCommand.Parameters.Add(startTime);
            this.SqlCommand.Parameters.Add(endTime);

            return this.ExecuteNonQuery();
        }//end of protected override void CreateUpdateParams(UserManagement item)
    }//end of public class RoomScheduleRepository : Repository<RoomSchedule>
}//end of namespace ABS.DAL.AdoNet